/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class FichaDAOImpl implements orm.dao.FichaDAO {
	public Ficha loadFichaByORMID(int id) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return loadFichaByORMID(session, id);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha getFichaByORMID(int id) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return getFichaByORMID(session, id);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByORMID(int id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return loadFichaByORMID(session, id, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha getFichaByORMID(int id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return getFichaByORMID(session, id, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByORMID(PersistentSession session, int id) throws PersistentException {
		try {
			return (Ficha) session.load(orm.Ficha.class, new Integer(id));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha getFichaByORMID(PersistentSession session, int id) throws PersistentException {
		try {
			return (Ficha) session.get(orm.Ficha.class, new Integer(id));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByORMID(PersistentSession session, int id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Ficha) session.load(orm.Ficha.class, new Integer(id), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha getFichaByORMID(PersistentSession session, int id, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Ficha) session.get(orm.Ficha.class, new Integer(id), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha[] listFichaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return listFichaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha[] listFichaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return listFichaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha[] listFichaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Ficha as Ficha");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Ficha[]) list.toArray(new Ficha[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha[] listFichaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Ficha as Ficha");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Ficha[]) list.toArray(new Ficha[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return loadFichaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return loadFichaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Ficha[] fichas = listFichaByQuery(session, condition, orderBy);
		if (fichas != null && fichas.length > 0)
			return fichas[0];
		else
			return null;
	}
	
	public Ficha loadFichaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Ficha[] fichas = listFichaByQuery(session, condition, orderBy, lockMode);
		if (fichas != null && fichas.length > 0)
			return fichas[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateFichaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return iterateFichaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateFichaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.PruebaPDSPersistentManager.instance().getSession();
			return iterateFichaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateFichaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Ficha as Ficha");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateFichaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Ficha as Ficha");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha createFicha() {
		return new orm.Ficha();
	}
	
	public boolean save(orm.Ficha ficha) throws PersistentException {
		try {
			orm.PruebaPDSPersistentManager.instance().saveObject(ficha);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Ficha ficha) throws PersistentException {
		try {
			orm.PruebaPDSPersistentManager.instance().deleteObject(ficha);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Ficha ficha)throws PersistentException {
		try {
			if(ficha.getPrestacion() != null) {
				ficha.getPrestacion().ficha.remove(ficha);
			}
			
			if(ficha.getPacienterut() != null) {
				ficha.getPacienterut().ficha.remove(ficha);
			}
			
			return delete(ficha);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Ficha ficha, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(ficha.getPrestacion() != null) {
				ficha.getPrestacion().ficha.remove(ficha);
			}
			
			if(ficha.getPacienterut() != null) {
				ficha.getPacienterut().ficha.remove(ficha);
			}
			
			try {
				session.delete(ficha);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Ficha ficha) throws PersistentException {
		try {
			orm.PruebaPDSPersistentManager.instance().getSession().refresh(ficha);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Ficha ficha) throws PersistentException {
		try {
			orm.PruebaPDSPersistentManager.instance().getSession().evict(ficha);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Ficha loadFichaByCriteria(FichaCriteria fichaCriteria) {
		Ficha[] fichas = listFichaByCriteria(fichaCriteria);
		if(fichas == null || fichas.length == 0) {
			return null;
		}
		return fichas[0];
	}
	
	public Ficha[] listFichaByCriteria(FichaCriteria fichaCriteria) {
		return fichaCriteria.listFicha();
	}
}
